package com.yanqu.road.server.cmd.activity.relation.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.activity.RelationActivityRankMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_RELATION_ACTIVITY_QUERY_MEMBER_CONDITION, desc = "跨服来查询跨服商战商会成员贡献值")
public class CrossRelationQueryMemberConditionCmd extends AbstractCommand {

	private int activityId;

	private String unionUid;

	private long serverId;        //发起查询的区服编号

	private long userId;          //发起查询的玩家编号

	private UnionActivityConditionData unionActivityConditionData;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CookBoyActivityProto.CrossCookBoyActivityConditionUnionMemberDataReqMsg reqMsg = CookBoyActivityProto.CrossCookBoyActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		serverId = reqMsg.getServerId();
		userId = reqMsg.getUserId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		unionActivityConditionData = RelationActivityRankMgr.getUnionActivityConditionData(activityId, unionUid);
		if (null == unionActivityConditionData) {
			ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
			return;
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		CookBoyActivityProto.CrossCookBoyConditionUnionMemberDataRespMsg.Builder respMsg = CookBoyActivityProto.CrossCookBoyConditionUnionMemberDataRespMsg.newBuilder();
		respMsg.setActivityId(activityId);
		CookBoyActivityProto.CrossCookBoyConditionUnionMemberDataMsg.Builder memberMsg = CookBoyActivityProto.CrossCookBoyConditionUnionMemberDataMsg.newBuilder();
		memberMsg.setRet(ret);
		memberMsg.setServerId(serverId);
		memberMsg.setUserId(userId);
		if (0 == ret) {
			if (null != unionActivityConditionData) {
				ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityPb.parseActivityConditionUnionMemberDataListMsg(
						UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, RelationActivityRankMgr.getUserActivityConditionDataMap(activityId)));
				memberMsg.setDataList(dataMsg);
			}
		}
		respMsg.setDataMsg(memberMsg);
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_RELATION_ACTIVITY_MEMBER_CONDITION_DATA_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}

}
